package com.example.lending.View.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lending.Model.Operation;
import com.example.lending.R;

import java.util.List;

public class OperationAdapter extends RecyclerView.Adapter<OperationAdapter.MyViewHolder> {
    private Context context;
    private List<Operation> operations;

    public OperationAdapter(Context context, List<Operation> operations) {
        this.context = context;
        this.operations = operations;
    }

    @Override
    public int getItemCount() {
        if(operations==null)
            return 0;
        return operations.size();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_record_operation,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if(holder instanceof MyViewHolder){
            Operation operation=operations.get(position);
            holder.tvName.setText("用户名："+operation.getUsername());
            holder.tvDuration.setText("行为：："+operation.getOperation());
            holder.tvTime.setText("操作时间："+operation.getTime());
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvName,tvDuration,tvTime;

        public MyViewHolder(View itemView){
            super(itemView);
            tvName=itemView.findViewById(R.id.tv_nameoperation);
            tvTime=itemView.findViewById(R.id.tv_timeoperation);
            tvDuration=itemView.findViewById(R.id.tv_durationoperation);
        }
    }
}
